<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Karaoke extends MY_Controller {
	private $xml_songs = 'public/data/songs.xml';
	public function __construct() {
		parent::__construct();
        
        $this->template = 'mobile/layout';
        $this->load->model('Karas_model');
        $this->load->library('pagination');
        $this->load->library('session');
	}
	
	public function home() {
		$this->view['template'] = 'mobile/home';
		$data = $this->Karas_model->get_limit($this->per_page, 
				$this->uri->segment(4) ? $this->uri->segment(4) :0);
		$total = $this->Karas_model->count_all();
		
		$this->view['data'] = array (
			'songs' 		=> $data->result_array(),
			'pagination'	=> $this->_pagination(
				'ma-so-karaoke/page',
				$total ,
				$this->per_page,
				4	
			),
			'keyword' 		=> '',
			'total'			=> $this->count_total_page($total),
			'page'			=> $this->uri->segment(4) ? $this->uri->segment(4) :1,
			'action'		=> 'home',
			'see_more_button'	=> $total > $this->per_page		 				
		);
		
		$this->_render($this->view);		
	}
	
    public function phpinfo() {
        echo phpinfo();
        exit;
    }
    
    public function search() {
            $key = str_replace('+', ' ', $this->input->get('keyword'));
            $this->view['template'] = 'mobile/home';
            $page = $this->input->get('page');
            
            $offset = $page ? ($page - 1) * $this->per_page : 0;

            $vol = $this->input->get('vol');
           
            $data = $this->Karas_model->search($key, " LIMIT {$this->per_page} offset {$offset}", $vol);
               
            if ($this->input->is_ajax_request()) {
                echo json_encode($data->result_array());
                exit;
            }

            if ($vol) {
                $action = 'vol';
                $base_url = 'ma-so-karaoke/tim-kiem.html?vol='. $vol;
            } else {
                $base_url = 'ma-so-karaoke/tim-kiem.html?keyword='. $key;
                $action = 'keyword';
            }
            
            $total = $this->Karas_model->count_all_search($key, $vol);
            
            $this->view['data'] = array (
                            'songs'  		=> $data->result_array(),
                            'pagination'	=> $this->_pagination(
                                                    $base_url,
                                                    $total,
                                                    $this->per_page,
                                                    4,
                                                    true
                                            ),
                            'keyword'		=> $key,
                            'total'			=> $this->count_total_page($total),
                            'page'			=> $this->uri->segment(4) ? $this->uri->segment(4) :1,
                            'action'		=> $action,
                            'vol'			=> $vol,
                            'see_more_button'	=> $total > $this->per_page
            );
            $this->_render($this->view);
    }
    
    
    public function _search() {
        $key = str_replace('+', ' ', $this->input->get('keyword'));
        $this->view['template'] = 'mobile/home';
        $page = $this->input->get('page');

        $offset = $page ? ($page - 1) * $this->per_page : 0;

        $vol = $this->input->get('vol');
        if ($vol) {
            $action = 'vol';
            $base_url = 'karaoke/search/?vol='. $vol;
        } else {
            $base_url = 'karaoke/search/?keyword='. $key;
            $action = 'keyword';
        }

        //$data = $this->Karas_model->search($key, " LIMIT {$this->per_page} offset {$offset}", $vol);
        $xpath_query = $this->Karas_model->get_xpath_query(array('keyword' => $key, 'vol' => $vol));

        $xml = simplexml_load_file($this->xml_songs);
        $data = $xml->xpath($xpath_query);

        if ($this->input->is_ajax_request()) {
            echo json_encode(array_slice($data, $offset, $this->per_page));
            exit;
        }

        //$total = $this->Karas_model->count_all_search($key, $vol);
        $total = count($data);

        $this->view['data'] = array (
                        'songs'  		=> array_slice($data, $offset, $this->per_page),
                        'pagination'	=> $this->_pagination(
                                                $base_url,
                                                $total,
                                                $this->per_page,
                                                4,
                                                true
                                        ),
                        'keyword'		=> $key,
                        'total'			=> $this->count_total_page($total),
                        'page'			=> $this->uri->segment(4) ? $this->uri->segment(4) :1,
                        'action'		=> $action,
                        'vol'			=> $vol,
                        'see_more_button'	=> $total > $this->per_page
        );
        $this->_render($this->view);
    }
    
    public function get_more() {
        $offset = $this->input->get('num_displayed');
        $action = $this->input->get('action');
        if ($this->input->is_ajax_request()) {
            
            if ($action == 'home') {
                $data = $this->Karas_model->get_limit_offset($this->per_page, $offset);
                $total = $this->Karas_model->count_all();
            } else if ($action == 'keyword') {
                $key = $this->input->get('keyword');
                $data = $this->Karas_model->search($key, " LIMIT {$this->per_page} offset {$offset}");

                $total = $this->Karas_model->count_all_search($key, '');
                
            } else {
                $vol = $this->input->get('vol');
                $data = $this->Karas_model->search('', " LIMIT {$this->per_page} offset {$offset}", $vol);
                $total = $this->Karas_model->count_all_search('', $vol);
            }

            $view['data'] = array('songs' => $data->result_array());	
            $data = $this->load->view('mobile/common/_song-item',$view, TRUE);

            $see_more = $total > ($offset + $this->per_page);

            echo json_encode(array(
                    'data' => $data,
                    'see_more_btn'	=> $see_more
            ));
            exit;
        }
    }
}